import {
	jsonp
} from 'vue-jsonp';
//app直接获取地理位置，不需要发起请求
export function getLocationInfo() { //2. 获取地理位置
	// #ifdef H5
	// 适配h5请求跨域问题
	return new Promise((resolve, reject) => {

		uni.getLocation({
			type: 'gcj02',
			success: async (res) => {
				const latitude = res.latitude.toString();
				const longitude = res.longitude.toString();
				const url = 'https://apis.map.qq.com/ws/geocoder/v1';
				// console.log(res,'res(((((())))11))')
				//通过经纬度换取地址
				await jsonp(url, {
					key: 'LAVBZ-SOF3V-LK2PG-5DBRG-5WOKF-F6B6B',
					location: `${latitude},${longitude}`,
					output: 'jsonp',
				}).then(re => {
					let address = {
						address: re.result.address_component,
						latitude: latitude,
						longitude: longitude
					};
					uni.setStorage({ //将经纬度保存到本地
						key: 'site',
						data: address
					});
					resolve(address);
				}).catch(err => {
					console.log(err);
					reject(err);
				});
			},
			fail: async (res) => {
				console.log(res);
				reject(res);
			}
		});
	});
	// #endif
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			success(res) {
				let latitude, longitude;
				latitude = res.latitude.toString();
				longitude = res.longitude.toString();
				uni.request({
					header: {
						"Content-Type": "application/text"
					},
					url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' +
						longitude + '&key=LAVBZ-SOF3V-LK2PG-5DBRG-5WOKF-F6B6B',
					success(re) {
						if (re.statusCode === 200 && re.data.status === 0) {
							let address = {
								address: re.data.result.address_component,
								latitude: latitude,
								longitude: longitude
							};
							uni.setStorage({
								key: 'site',
								data: address
							});
							resolve(address);
						} else {
							let address = {
								address: undefined,
								latitude: latitude,
								longitude: longitude
							};
							resolve(address);
						}
					},
					fail(e) {
						let address = {
							address: undefined,
							latitude: latitude,
							longitude: longitude
						};
						resolve(address);
					}
				});
			},
			fail(res) {
				reject(res);
			}
		});
	})

}


//删除省，市，区，县
export function place(data) {
	if (data.charAt(data.length - 1) == '省' || data.charAt(data.length - 1) ==
		'市' || data.charAt(data.length -
			1) ==
		'区' || data.charAt(data.length - 1) == '县') {
		return data.substr(0, data.length - 1);
	} else {
		return data;
	}
}


//删除省，市，区，县
export function geocoder(latitude, longitude) {
	return new Promise((resolve, reject) => {
		uni.request({
			header: {
				"Content-Type": "application/text"
			},
			url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' +
				longitude + '&key=LAVBZ-SOF3V-LK2PG-5DBRG-5WOKF-F6B6B',
			success(re) {
				if (re.statusCode === 200 && re.data.status === 0) {
					console.log(re);
					let address = {
						address: re.data.result.address_component,
						latitude: latitude,
						longitude: longitude
					};
					uni.setStorage({
						key: 'site',
						data: address
					});
					resolve(address);
				} else {
					let address = {
						address: undefined,
						latitude: latitude,
						longitude: longitude
					};
					resolve(address);
				}
			},
			fail(e) {
				let address = {
					address: undefined,
					latitude: latitude,
					longitude: longitude
				};
				resolve(address);
			}
		});
	});
}